package com.photography.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.photography.entity.PageResult;
import com.photography.mapper.ProductMapper;
import com.photography.mapper.SerialMapper;
import com.photography.pojo.Serial;
import com.photography.service.SerialService;
import com.photography.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class SerialServiceImpl implements SerialService {

    @Autowired
    private SerialMapper serialMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private IdWorker idWorker;


    @Override
    public Serial findById(String id) {
        Serial serial = serialMapper.selectById(id);
        List<String> ids = serialMapper.findProductIdBySerialId(id);
        serial.setProductIds(ids);
        return serial;
    }

    @Override
    public void addSerial(Serial serial) {
        String serialId = idWorker.nextId() + "";
        serial.setId(serialId);
        serial.setCreated(new Date());
        serialMapper.insert(serial);
        List<String> productIds = serial.getProductIds();
        if (!CollectionUtils.isEmpty(productIds)){
            for (String productId : productIds) {
                serialMapper.addSerialAndProduct(serialId,productId);
            }
        }
    }

    @Override
    public void deleteSerial(String id) {
        serialMapper.deleteById(id);
    }

    @Override
    public void updateSerial(Serial serial) {
        Integer integer = serialMapper.updateById(serial);
        if (integer >0){
            serialMapper.deleteSerialAndProduct(serial.getId());
            List<String> productIds = serial.getProductIds();
            if (!CollectionUtils.isEmpty(productIds)){
                for (String productId : productIds) {
                    serialMapper.addSerialAndProduct(serial.getId(),productId);
                }
            }
        }
    }

    @Override
    public List<Serial> findSerialByOrderId(String orderId) {
        return serialMapper.findSerialByOrderId(orderId);
    }


    @Override
    public void updateStatusSerial(String serialId) {
        Serial serial = new Serial();
        serial.setId(serialId);
        serial.setStatus(3);
        serialMapper.updateById(serial);
        serialMapper.deleteSerialAndProduct(serialId);
    }

    @Override
    public PageResult<Serial> findAll(Serial serial, Integer page, Integer size) {
        Page<Serial> Page = new Page<>(page, size);
        QueryWrapper<Serial> wrapper = new QueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(serial.getSerialname()),"serialname",serial.getSerialname())
                .eq("status","1")
                .or()
                .eq("status","2");
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Serial> serialPage = serialMapper.selectPage(Page, wrapper);
        PageResult<Serial> serialPageResult = new PageResult<>();
        serialPageResult.setTotal(serialPage.getTotal());
        serialPageResult.setRows(serialPage.getRecords());
        return serialPageResult;
    }

    @Override
    public List<Serial> findAllList() {
        return serialMapper.selectList(new QueryWrapper<Serial>().eq("status","1").or().eq("status","0"));
    }
}
